<template>
  <div>
    <div class="d-flex">
      <h4 class="panel-title">
        <span>我的任务</span>
        <span class="panel-title-num" v-show="taskTotal">{{ taskTotal }}</span>
      </h4>
    </div>

    <el-table :data="tableData" v-loading="loading" border class="table-sm">
      <el-table-column label="任务名称" prop="taskName" align="center"></el-table-column>
      <el-table-column label="未处理/已处理" prop="taskCount" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.taskCount }}/{{ scope.row.process }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="showDialog(scope.row)"
            icon="el-icon-edit"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- <task-project-approval :visible.sync="approvalVisible" type="3"></task-project-approval>

    <task-project-item :visible.sync="itemVisible" type="4"></task-project-item> -->

    <task-project-investigate
      :visible.sync="investigateIngVisible"
      type="5,6"
    ></task-project-investigate>

    <task-register-dialog :visible.sync="registerVisible" type="17"></task-register-dialog>

    <!-- <task-fee-pay-dialog :visible.sync="feeVisible"
                         type="10"></task-fee-pay-dialog>
    <task-fee-arrival :visible.sync="arrivalVisible"
                      type="11"></task-fee-arrival> -->

    <contrack-approval-dialog :visible.sync="contrackApproval" type="35"></contrack-approval-dialog>
    <loan-approval-dialog :visible.sync="loanApproval" type="36"></loan-approval-dialog>

    <task-handle-contract-dialog
      :visible.sync="handleContractVisible"
      type="42"
    ></task-handle-contract-dialog>

    <task-fee :visible.sync="payVisible"></task-fee>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import { enumTaskType } from '@/credit/plugins/enumTown'
// import TaskProjectApproval from '@/credit/views/county/index/myTask/TaskProjectApproval'
// import TaskProjectItem from '@/credit/views/county/index/myTask/TaskProjectItem'
import TaskProjectInvestigate from '@/credit/views/county/index/myTask/TaskProjectInvestigate'
// import TaskContrackDialog from '@/credit/views/county/index/myTask/TaskContrackDialog'
import TaskRegisterDialog from '@/credit/views/county/index/myTask/TaskRegisterDialog'
// import TaskFeePayDialog from '@/credit/views/county/index/myTask/TaskFeePayDialog'
// import TaskFeeArrival from '@/credit/views/county/index/myTask/TaskFeeArrival'
import TaskHandleContractDialog from '@/credit/views/city/index/myTask/TaskHandleContractDialog'
import ContrackApprovalDialog from '@/credit/views/county/index/myTask/ContrackApprovalDialog'
import LoanApprovalDialog from '@/credit/views/county/index/myTask/LoanApprovalDialog'
import TaskFee from '@/credit/views/county/index/myTask/TaskFee'
const enumConcat = {
  56: '调查审批',
  101112: '保费结算',
}
export default {
  components: {
    // TaskProjectApproval,
    // TaskProjectItem,
    TaskProjectInvestigate,
    // TaskContrackDialog,
    TaskRegisterDialog,
    ContrackApprovalDialog,
    LoanApprovalDialog,
    TaskHandleContractDialog,
    // TaskFeePayDialog,
    // TaskFeeArrival,
    // TaskReportDialog,
    TaskFee,
  },
  props: {
    height: {
      type: Number,
      default: 300,
    },
  },
  data() {
    return {
      params: {
        index: 1,
        size: 8,
      },
      tableData: [],
      enumTaskType,
      loading: false,
      approvalVisible: false,
      itemVisible: false,
      investigateVisible: false,
      investigateIngVisible: false,
      // letterVisible: false,
      // contrackVisible: false,
      registerVisible: false,
      feeVisible: false,
      arrivalVisible: false,
      reportVisible: false,
      contrackApproval: false,
      loanApproval: false,
      handleContractVisible: false,
      payVisible: false,
    }
  },
  computed: {
    ...mapGetters(['myTaskCount', 'job']),
    tasks() {
      const data = []
      const mustKeys = [56, 35, 42, 36, 101112]
      //, 12, 13
      mustKeys.forEach(item => {
        data.push({
          taskType: item,
          taskName: enumTaskType[item] || enumConcat[item],
          taskCount: this.getNum(item),
          process: this.getProcess(item),
        })
      })
      return data
    },
    totalCount() {
      return this.tasks.length
    },
    taskTotal() {
      return this.tasks.reduce((prev, curr) => prev + curr.taskCount, 0)
    },
  },
  watch: {
    tasks: {
      handler: function () {
        this.setData()
      },
      deep: true,
      immediate: true,
    },
  },
  created() {
    this.getData()
  },
  mounted() {},
  methods: {
    getNum(type) {
      if (type === 56) {
        return this.getNum(5) + this.getNum(6)
      } else if (type === 101112) {
        return this.getNum(10) + this.getNum(11) + this.getNum(12)
      }
      return (this.myTaskCount && this.myTaskCount[type] && +this.myTaskCount[type].num) || 0
    },
    getProcess(type) {
      if (type === 56) {
        return this.getProcess(5) + this.getProcess(6)
      } else if (type === 101112) {
        return this.getProcess(10) + this.getProcess(11) + this.getProcess(12)
      }
      return (this.myTaskCount && this.myTaskCount[type] && +this.myTaskCount[type].process) || 0
    },
    setData() {
      this.tableData = this.tasks.slice(
        (this.params.index - 1) * this.params.size,
        this.params.index * this.params.size
      )
    },
    async getData() {
      this.loading = true
      await this.$utils.promise(this.$store.dispatch('getMyTaskCount'))
      this.loading = false
    },
    showDialog(row) {
      switch (+row.taskType) {
        // case 3:
        //   this.approvalVisible = true
        //   break
        // case 4:
        //   this.itemVisible = true
        //   break
        // case 5:
        //   this.investigateVisible = true
        //   break
        case 56:
          this.investigateIngVisible = true
          break
        case 35:
          this.contrackApproval = true
          break
        case 36:
          this.loanApproval = true
          break
        // case 7:
        //   this.letterVisible = true
        //   break
        // case 8:
        //   this.contrackVisible = true
        //   break
        // case 17:
        //   this.registerVisible = true
        //   break
        // case 11:
        //   this.arrivalVisible = true
        //   break
        // case 13:
        //   this.reportVisible = true
        //   break
        case 42:
          this.handleContractVisible = true
          break
        case 101112:
          this.payVisible = true
          break
        default:
          break
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.panel-title {
  position: relative;
  font-size: 1.142857rem;

  .panel-title-num {
    position: absolute;
    right: -1rem;
    padding: 0 0.21rem;
    border-radius: 1rem;
    background-color: red;
    font-size: 10px;
    color: #fff;
    line-height: 1;
    display: inline-block;
    text-align: center;
  }
}
</style>
